package com.lm.controller;

import com.lm.entity.Student;
import com.lm.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
@Controller
public class StudentController {
    @Autowired
    private IStudentService studentService;

    @GetMapping("student/{rollNum}")
    public ResponseEntity<Student> getStudentByRollNum(@PathVariable("rollNum") Long rollNum) {
        Student student = studentService.getStudentByRollNum(rollNum);
        return new ResponseEntity<Student>(student, HttpStatus.OK);
    }
    @GetMapping("student/byName")
    public ResponseEntity<Student> getStudentsByName(@RequestParam String name) {
        Student students = studentService.getStudentByName(name);
        return new ResponseEntity<>(students, HttpStatus.OK);
    }
    @GetMapping("students")
    public ResponseEntity<List<Student>> getAllStudents() {
        List<Student> list = studentService.getAllStudents();
        return new ResponseEntity<List<Student>>(list, HttpStatus.OK);
    }

    @PostMapping("student/add")
    public ResponseEntity<Void> addStudent(@RequestBody Student student) {
        boolean b = studentService.addStudent(student);
        return new ResponseEntity<Void>(HttpStatus.CREATED);
    }

    @PostMapping("student/update")
    public ResponseEntity<Student> updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        return new ResponseEntity<Student>(student, HttpStatus.OK);
    }

    @PostMapping("student/delete")
    public ResponseEntity<Void> deleteStudent(@RequestParam Long rollNum) {
        studentService.deleteStudent(rollNum);
        return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
    }
    @GetMapping("/shutdown")
    public String shutdown() {
        try {
            // 定义命令来查找并关闭8081端口上的进程
            String findProcessCommand = "netstat -ano | findstr :8081";
            Process findProcess = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", findProcessCommand});

            BufferedReader reader = new BufferedReader(new InputStreamReader(findProcess.getInputStream()));
            String line;
            String pid = null;
            while ((line = reader.readLine()) != null) {
                if (line.contains("LISTENING")) {
                    String[] tokens = line.trim().split("\\s+");
                    pid = tokens[tokens.length - 1];
                    break;
                }
            }

            if (pid != null) {
                // 杀死进程的命令
                String killCommand = "taskkill /PID " + pid + " /F";
                Process killProcess = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", killCommand});
                killProcess.waitFor();
                return "成功终止PID: " + pid + "上的进程.";
            } else {
                return "未找到在8081端口上运行的进程.";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "关闭进程时发生错误: " + e.getMessage();
        }
    }
}
